package com.shushan.mixedexam.englishwords;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.mixedexam.MixedExamActivity;
import com.shushan.mixedexam.adapter.ExamFragmentAdapter;
import com.shushan.pages.newtype.EnglishWordsExerciseActivity;
import com.shushan.pages.newtype.bean.EnglishWordsEntryItemBean;
import com.shushan.shushanbao.R;
import com.shushan.ui.NoScrollViewPager;
import com.shushan.util.DownloadUtil;
import com.shushan.util.ExamAudioPlayUtil;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: EnglishWordsStudyActivity
 * @Desciption: //单词练习学习界面
 * @author: zhangshihao
 * @date: 2018-07-31
 */
public class EnglishWordsStudyActivity extends BaseActivity implements View.OnClickListener,
OnTrumpetClickListener,ViewPager.OnPageChangeListener{

    private static final String TAG = EnglishWordsStudyActivity.class.getSimpleName();

    private NoScrollViewPager examViewpager;
    private ExamFragmentAdapter examFragmentAdapter;
    private List<Fragment> studyFragmentList;
    private TextView tvCurrentNum;
    private TextView tvTopicTotalNum;
    private Button btnNextTopic;

    private int position = 0;
    private int outline_detail_id = 0;
    private List<EnglishWordsEntryItemBean> entryItemBeans;
    private int currentItemNum = 0;

    private Handler mScrollHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                examViewpager.setCurrentItem(msg.arg1,true);
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_english_words_study);

        ScreenManager sm = new ScreenManager();
        if(!sm.setDeepStatusBar(true,this)){
            findViewById(R.id.titlebar_divider).setVisibility(View.GONE);
        }

        entryItemBeans = (List<EnglishWordsEntryItemBean>)Session.getSession().get("entryItemBeans");

        outline_detail_id = getIntent().getIntExtra("outline_detail_id",-1);
        position = getIntent().getIntExtra("position",-1);

        initView();

        initData();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"onStop()");
        ExamAudioPlayUtil.stop();
    }

    private void initView(){
        examViewpager = findViewById(R.id.exam_vp);
        examViewpager.setScroll(true);
        examViewpager.addOnPageChangeListener(this);
        tvCurrentNum = findViewById(R.id.tv_current_num);
        tvTopicTotalNum = findViewById(R.id.tv_topic_total_num);
        btnNextTopic = findViewById(R.id.btn_next_topic);
        btnNextTopic.setOnClickListener(this);
    }

    private void initData(){
        if(ToolUtils.isListEmpty(entryItemBeans)){
            Log.d(TAG,"downloadAudioData : entryItemBeans为空");
            return;
        }

        studyFragmentList = getFragments();
        examFragmentAdapter = new ExamFragmentAdapter(this,getSupportFragmentManager(),studyFragmentList);
        examViewpager.setAdapter(examFragmentAdapter);
        tvTopicTotalNum.setText("/"+studyFragmentList.size()+")");
        if(position >= 0){
            currentItemNum = position;
            Message msg = mScrollHandler.obtainMessage(0);
            msg.arg1 = currentItemNum;
            mScrollHandler.sendMessage(msg);
            tvCurrentNum.setText("("+String.valueOf(currentItemNum+1));
            if(currentItemNum + 1 == examFragmentAdapter.getCount()){
                btnNextTopic.setText("开始练习");
                btnNextTopic.setBackgroundResource(R.drawable.bg_white_orange_radius_50);
            }

            if(entryItemBeans != null && studyFragmentList != null){
                EnglishWordsEntryItemBean entryItemBean = entryItemBeans.get(position);
                EnglishWordsStudyFragment fragment = (EnglishWordsStudyFragment) studyFragmentList.get(position);
                if(entryItemBean != null && fragment != null){
                    ExamAudioPlayUtil.playAudio(DownloadUtil.getEnglishWordsStudyAudioPath(outline_detail_id,entryItemBean.id),fragment.getPlayAnim());
                }
            }
        }
    }

    private List<Fragment> getFragments(){
        List<Fragment> fragments = new ArrayList<>();
        for(int i=0;i<entryItemBeans.size();i++){
            EnglishWordsStudyFragment fragment = EnglishWordsStudyFragment.newInstance(i,entryItemBeans.get(i));
            fragment.setOnTrumpetClickListener(this);
            fragments.add(fragment);
        }
        return fragments;
    }

    @Override
    public void onPageSelected(int position) {
        currentItemNum = position;
        tvCurrentNum.setText("("+String.valueOf(currentItemNum+1));
        if(currentItemNum + 1 == examFragmentAdapter.getCount()){
            btnNextTopic.setText("开始练习");
            btnNextTopic.setBackgroundResource(R.drawable.bg_white_orange_radius_50);
        }else{
            btnNextTopic.setText("下一题");
            btnNextTopic.setBackgroundResource(R.drawable.btn_next_topic_selector);
        }

        Log.i(TAG,"onPageSelected position=" + position);
        if(entryItemBeans != null && studyFragmentList != null){
            EnglishWordsEntryItemBean entryItemBean = entryItemBeans.get(position);
            EnglishWordsStudyFragment fragment = (EnglishWordsStudyFragment) studyFragmentList.get(position);
            if(entryItemBean != null && fragment != null){
                ExamAudioPlayUtil.playAudio(DownloadUtil.getEnglishWordsStudyAudioPath(outline_detail_id,entryItemBean.id),fragment.getPlayAnim());
            }
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //do nothing
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //do nothing
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_next_topic://下一题
                if(currentItemNum + 1 == examFragmentAdapter.getCount()){
                    Intent intent = new Intent(EnglishWordsStudyActivity.this, MixedExamActivity.class);
                    intent.putExtra("examType", Constants.TYPE_ENGLISH_WORDS_EXERCISE);
                    startActivityAllow(intent);
                    break;
                }
                Message msg = mScrollHandler.obtainMessage(0);
                msg.arg1 = currentItemNum + 1;
                mScrollHandler.sendMessage(msg);
                tvCurrentNum.setText("("+String.valueOf(currentItemNum+1));
                break;
        }
    }

    @Override
    public void onTrumpetClick(int wordId, AnimationDrawable playAnim) {
        ExamAudioPlayUtil.playAudio(DownloadUtil.getEnglishWordsStudyAudioPath(outline_detail_id,wordId),playAnim);
    }

}
